package yuejian_07.yuejian_07.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import yuejian_07.yuejian_07.R;
import yuejian_07.yuejian_07.RetrofitListener;
import yuejian_07.yuejian_07.adapter.ShowListAdapter;
import yuejian_07.yuejian_07.base.RetrofitListenerTow;
import yuejian_07.yuejian_07.javabean.Show;
import yuejian_07.yuejian_07.javabean.User;
import yuejian_07.yuejian_07.model.ShowModel;
import yuejian_07.yuejian_07.model.UserModel;

import static yuejian_07.yuejian_07.base.Constant.PHOTO_URL;

public class UserInfoActivity extends Activity implements RetrofitListener<User>,RetrofitListenerTow<List<Show>> {

    ImageView u_pic,img_return;
    TextView u_name,u_age,u_intro,u_sex;

    private RecyclerView recyclerView;

    int user_id;
    String user_name;
    String user_pic;
    String user_intro;
    String user_sex;
    Object user_age;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_user_info);
        initViews();
        initEvents();
        initData();
    }

    public void initViews(){
        u_name=(TextView)findViewById(R.id.username);
        u_intro=(TextView)findViewById(R.id.userintro);
        u_sex=(TextView)findViewById(R.id.sex);
        u_age=(TextView)findViewById(R.id.age);
        img_return=(ImageView)findViewById(R.id.img_return);
        u_pic=(ImageView)findViewById(R.id.userpic);
        recyclerView= (RecyclerView)findViewById(R.id.rv_usershows);

    }

    public void initEvents(){
        img_return.setOnClickListener(new View.OnClickListener() {//返回
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());

    }

    public void initData(){

        getUserInfo();
        getShowsList();
    }

    //获取用户信息数据
    public void getUserInfo(){

        Bundle bundle = getIntent().getExtras();
        user_id = bundle.getInt("user_id");
        System.out.printf("user_id");
        UserModel userModel = new UserModel();
        userModel.getUserById(user_id,this);

    }
    //获取晒图列表数据
    public void getShowsList(){
        ShowModel showModel=new ShowModel();
        showModel.getShowByUser(user_id,this);
    }

    @Override
    public void onSuccess(User user){
        user_name = user.getUser_name();
        user_pic =PHOTO_URL+user.getUser_pic();
        user_age = user.getUser_age();
        user_intro=user.getUser_intro();
        user_sex=user.getUser_sex();
        u_name.setText(user_name);
        u_intro.setText(user_intro);
        u_age.setText(user_age+" ");
        u_sex.setText(user_sex);
        //t_ask.setText(train_phone+"");
        if (user_pic != "") {
            Picasso.with(UserInfoActivity.this)
                    .load(user_pic)
                    .into(u_pic);
            u_pic.setScaleType(ImageView.ScaleType.FIT_XY);
        } else {
            u_pic.setImageResource(R.drawable.no_pic);
        }

    }
    @Override
    public void onFail(){ Toast.makeText(UserInfoActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();}

    @Override
    public void onSuccessTow(List<Show> data ){

        //System.out.printf("data");
        recyclerView.setAdapter(new ShowListAdapter(UserInfoActivity.this,data));
    }

    @Override
    public void onFailTow(){ Toast.makeText(UserInfoActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();}
}
